package com.tuo51.childcare.controller;


import com.tuo51.evaluate.domain.childrens.Children;
import com.tuo51.core.domain.staffs.Staff;
import com.tuo51.evaluate.service.ChildrenService;
import com.tuo51.evaluate.service.EvaluateV2Service;
import com.tuo51.foudation.exception.BusinessException;
import com.tuo51.foudation.model.ErrorCode;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import com.tuo51.foudation.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

import static com.tuo51.foudation.constant.RequestConstant.TICKET;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * 测评小程序使用的接口
 */

@RestController
@RequestMapping("/papi/children")
@Api(value = "/papi/children", tags = "家长服务号 - 宝宝")
public class PAPIChildrenController {

    @Autowired
    private EvaluateV2Service evaluateService;

    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private ChildrenService childrenService;


    @RequestMapping(value = "/mychildren", method = POST)
    public StatusResponse getChildren(@RequestHeader(TICKET) String ticket){
        Staff staff = cache.getStaff(ticket);

        Object ret = childrenService.getAllWithSourceByParent(staff);

        return StatusResponse.success(ret);
    }

    @RequestMapping(value = "/detail", method = POST)
    public StatusResponse childrenDetail(@RequestHeader(TICKET) String ticket, String childrenId){
        String staffId = cache.getStaffId(ticket);

        Children c = childrenService.getById(childrenId);
        if(c == null){
            throw new BusinessException(ErrorCode.CHILDREN_NOT_EXIST, "宝宝不存在");
        }

        c.setMonth(DateUtil.betweenMonths(new Date(), c.getBirthday()));

        return StatusResponse.success(c);
    }

    @RequestMapping(value = "/delete", method = POST)
    public StatusResponse deleteChildren(@RequestHeader(TICKET) String ticket, String childrenId) {
        String staffId = cache.getStaffId(ticket);

        boolean ret = childrenService.deleteChildrenForStaff(childrenId, staffId);

        return StatusResponse.success(ret);
    }

    @ApiOperation("更新宝宝信息，所有字段可选")
    @RequestMapping(value = "/update_child_info", method = POST)
    public StatusResponse updateChildInfo(@RequestHeader(TICKET) String ticket,
                                          String childrenId,
                                          String childrenName,
                                          String nickName,
                                          String avatar,
                                          Integer gender,
                                          String birthday,
                                          String guardianPhone, String guardianName, String guardianRelation,
                                          String weight,
                                          String height,
                                          String header,
                                          String address,
                                          String remark){

        String staffId = cache.getStaffId(ticket);

        if (StringUtils.isEmpty(childrenId)){
            throw new BusinessException(ErrorCode.CHILDREN_NOT_EXIST, "宝宝不存在");
        }

        Children children = childrenService.getById(childrenId, staffId);
        if (children == null){
            throw new BusinessException(ErrorCode.CHILDREN_NOT_EXIST, "宝宝不存在");
        }

        children = childrenService.edit(children, childrenName, nickName, avatar, guardianPhone,guardianName, guardianRelation,
                birthday, gender, weight, height, header, address, remark);

        return StatusResponse.success(children);

    }

    @RequestMapping(value = "/default_children", method = GET)
    public StatusResponse defaultChildren(@RequestHeader(TICKET) String ticket){
        Staff staff = cache.getStaff(ticket);

        Children c = childrenService.defaultChildrenForParent(staff);

        return StatusResponse.success(c);
    }

    @RequestMapping(value = "/set_default_children", method = POST)
    public StatusResponse setDefaultChildren(@RequestHeader(TICKET) String ticket, String childrenId){
        Staff staff = cache.getStaff(ticket);

        Children c = childrenService.setDefaultChildren(staff,childrenId);

        return StatusResponse.success(c);
    }

    @RequestMapping(value = "/add_children", method = POST)
    public StatusResponse addChildren(@RequestHeader(TICKET) String ticket,
                                      String childAvatarUrl,
                                      String childrenName,
                                      String nickName,
                                      Integer gender,
                                      String birthday,
                                      String guardianPhone, String guardianName, String guardianRelation,
                                      String weight, String height){

        String staffId = cache.getStaffId(ticket);

        Children children = childrenService.add(staffId, childAvatarUrl,childrenName, nickName, gender,
                birthday, weight, height, guardianPhone, guardianName, guardianRelation);

        return StatusResponse.success(children);

    }
}
